var tab_id="",tab_title="",tab_content="";
var openTabMap=new Map();
var openCount=0;
$(function() {
    var tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>";
    var tabs = $( "#tabs" ).tabs();
    //添加事件
    $( "#add_tab").button().click(function() {
        addTab()
        });
    //添加tab页
    function addTab() {
        var label = tab_title,id = tab_id,
            li = $( tabTemplate.replace( /#\{href\}/g, "#" + id ).replace( /#\{label\}/g, label ) );
        tabs.find( ".ui-tabs-nav" ).append( li );
        tabs.append( "<div id='" + tab_id + "'>" + tab_content + "</div>" );
        tabs.tabs( "refresh" );
        tabs.tabs( "option", "active", openCount );
        openCount++;
    }
    // 关闭tab页时触发
    tabs.delegate( "span.ui-icon-close", "click", function() {
        var panelId = $( this ).closest( "li" ).remove().attr( "aria-controls" );
        var el = document.getElementById("tab_"+panelId), iframe = el.contentWindow;
        if(el) {
            if(!$.support.leadingWhitespace){
                el.src = 'about:blank';
                try {
                    iframe.document.write('');
                    iframe.close();
                } catch( e ) {
                }
            }
        }
        $( "#" + panelId ).remove();
        tabs.tabs( "refresh" );
        openTabMap.remove(panelId);
        openTabMap.each(function(key,val,i){
            openTabMap.put(key,i);
        });
        openCount--;
    });
});
// 添加一个新tab页
function addNewTab(tid,tname,pageurl){
    if(openTabMap.get(tid)==null){
        if(openCount>=9){
            alert("打开的菜单过多，最多只能打开9个菜单！");
            return;
        }
        tab_id=tid;
        tab_title=tname;
        var ifmHeight=pageHeight()-70;
        tab_content="<iframe src='"+pageurl+"' id='tab_"+tab_id+"' name='tab_"+tab_id+"' width='100%' height='"+ifmHeight+"' scrolling='auto' frameborder='0' border='0' framespacing='0'></iframe>";
        openTabMap.put(tab_id,openCount);
        document.getElementById("add_tab").click();
    }else{
        var tabs = $( "#tabs" ).tabs();
        tabs.tabs( "option", "active", openTabMap.get(tid));
    }
}
// TAB页变为活动的
function activeTab(tabId){
    if(openTabMap.get(tabId)==null){
        alert("TAB页"+tabId+"不存在！");
        return;
    }else{
        var tabs = $( "#tabs" ).tabs();
        tabs.tabs( "option", "active", openTabMap.get(tabId));
    }
}
// 根据菜单ID关闭TAB页
function removeTab(tabId){
    if(openTabMap.get(tabId)==null){
        alert("不存在的Tab页"+tabId);
        return null;
    }else{
        $( "#tabs" ).find( ".ui-tabs-nav li:eq("+openTabMap.get(tabId)+")" ).remove();
        var el = document.getElementById("tab_"+tabId), iframe = el.contentWindow;
        if(el) {
            if(!$.support.leadingWhitespace){
                el.src = 'about:blank';
                try {
                    iframe.document.write('');
                    iframe.close();
                } catch( e ) {
                }
            }
        }
        $( "#" + tabId ).remove();
        $( "tabs" ).tabs( "refresh" );
        openTabMap.remove(tabId);
        openTabMap.each(function(key,val,i){
            openTabMap.put(key,i);
        });
        openCount--;
    }
}
// 根据菜单ID获取TAB页页面对象
function getTab(tabId){
    if(openTabMap.get(tabId)==null){
        alert("不存在的Tab页");
        return null;
    }else{
        return document.frames("tab_"+tabId);
    }
}
// 关闭所有tab页
function closeTabs(){
    var acTab=new Map();
    openTabMap.each(function(key,val,i){
        acTab.put(key,val);
    });
    acTab.each(function(key,val,i){
        removeTab(key);
    });
}
function pageHeight(){
    if(!$.support.leadingWhitespace){
        return document.compatMode=="CSS1Compat"?document.documentElement.clientHeight:document.body.clientHeight;
    }else{
        return self.innerHeight;
    }
}
function pageWidth(){
    if(!$.support.leadingWhitespace){
        return document.compatMode=="CSS1Compat"?document.documentElement.clientWidth:document.body.clientWidth;
    }else{
        return self.innerWidth;
    }
}